package com.rcpcompany.examples.flows.directors;

import com.rcpcompany.examples.flows.pages.Page1Page;
import com.rcpcompany.examples.flows.pages.Page2Page;
import com.rcpcompany.ui.flows.AbstractFlowDirector;
import com.rcpcompany.ui.flows.IFlowDirector;
import com.rcpcompany.ui.flows.IFlowPage;

public class FirstDirector extends AbstractFlowDirector implements IFlowDirector {

	public FirstDirector(String name) {
		super(name);
	}

	protected IFlowPage page1 = new Page1Page(this);
	protected IFlowPage page2 = new Page2Page(this);

	protected int page = 1;
	private String myFirstName = "";
	private String myLastName = "";
	private String myCity = "";
	private String myCountry = "";
	private boolean myAddressWanted;

	@Override
	public boolean canNavigate(WhereTo where) {
		switch (where) {
		case PREVIOUS_STEP:
			return page > 1;
		case NEXT_STEP:
			switch (page) {
			case 1:
				return (myFirstName.length() > 0) && (myLastName.length() > 0);
			case 2:
				return (myCity.length() > 0) && (myCountry.length() > 0);
			}
		case FINISH:
			if (myAddressWanted) {
				return (myFirstName.length() > 0) && (myLastName.length() > 0) && (myCity.length() > 0)
						&& (myCountry.length() > 0);
			} else {
				return (myFirstName.length() > 0) && (myLastName.length() > 0);
			}
		case CANCEL:
			return true;
		}
		return false;
	}

	@Override
	public IFlowPage getCurrentPage() {
		switch (page) {
		case 1:
			return page1;
		case 2:
			return page2;
		}
		return null;
	}

	@Override
	public void navigate(WhereTo where) {
		switch (where) {
		case FIRST:
			page = 1;
			break;
		case PREVIOUS_STEP:
			page -= 1;
			return;
		case NEXT_STEP:
			if (myAddressWanted) {
				page += 1;
				if (page <= 2) return;
			}
		case FINISH:
		case CANCEL:
			myFirstName = "";
			myLastName = "";
			myCity = "";
			myCountry = "";
			getContainer().closeDirector(this);
			return;
		}
	}

	public void setName(String firstName, String lastName, boolean addressWanted) {
		myFirstName = firstName;
		myLastName = lastName;
		myAddressWanted = addressWanted;
	}

	public void setLocation(String city, String country) {
		myCity = city;
		myCountry = country;
	}

}
